﻿using System;
using System.Linq;
using System.Linq.Expressions;
using System.Collections.Generic;
using EAF.Utility;
using EAF.DBC;
using EAF.DBC.LogHandle;
using EAF.Cache;
using EAF.Server;
using App.Utility;
using App.Utility.ComEntity;
using App.Model;
using App.Model.Main;
using App.DTO;
using App.DTO.Main;
using App.IServer.Main;

namespace App.Server.Main
{
    /// <summary>
    /// 系统调试日志
    /// </summary>
    public class SysLogService : DBCBaseService<SysLog, AppMainContext>, ISysLogService
    {
        //context将赋值到DBC这里可注入其他db上下文 注入其他表服务可能造成死循环 建议使用GetTableService方法获取
        public SysLogService(AppMainContext context) : base(context)
        {

        }

        #region 其他可复用业务操作



        #endregion

        #region 增删改查等基础操作 相当于仓储层

        /// <summary>
        /// 添加 会记录日志
        /// </summary>
        /// <param name="entity">实体</param>
        /// <returns>插入记录id</returns>
        public int Add(SysLog entity)
        {
            var insId = DBC.Insert(entity);
            if (insId > 0)
            {
                entity.Id = insId;
                RecordBehavior(new RLogBehavior
                {
                    Table = nameof(SysLog),
                    Method = EntityMethod.M_添加,
                    Content = $"添加了ID为：{entity.Id}的记录",
                    Description = "CRUD操作"
                });
            }
            return insId;
        }

        /// <summary>
        /// 添加集合 会记录日志
        /// </summary>
        /// <param name="list">实体</param>
        /// <param name="insertIdentity">是否插入自增字段</param>
        /// <returns>插入记录id</returns>
        public int Add(List<SysLog> list, bool insertIdentity)
        {
            var count = DBC.InsertList(list, insertIdentity: insertIdentity);
            if (count > 0)
            {
                RecordBehavior(new RLogBehavior
                {
                    Table = nameof(SysLog),
                    Method = EntityMethod.M_添加,
                    Content = $"导入了{count}条数据，ID范围：{DBC.InsertListIdRange<SysLog>(count)}",
                    Description = "CRUD操作"
                });
            }
            return count;
        }

        /// <summary>
        /// 更新 会记录日志
        /// </summary>
        /// <param name="newEntity">新实体</param>
        /// <param name="oldEntity">旧实体 可不传</param>
        /// <returns>更新成功数量</returns>
        public int Update(SysLog newEntity, SysLog oldEntity = null)
        {
            if (oldEntity == null)
            {
                oldEntity = DBC.QueryFirst<SysLog>(newEntity.Id);
                if (oldEntity == null)
                {
                    return 0;
                }
            }

            newEntity.UpdateTime = DateTime.Now;

            var result = DBC.Update(newEntity);
            if (result > 0)
            {
                //处理修改记录
                var updMsg = DBC.UpdateLog(newEntity, oldEntity);
                RecordBehavior(new RLogBehavior
                {
                    Table = nameof(SysLog),
                    Method = EntityMethod.M_修改,
                    Content = $"修改了ID为：{newEntity.Id}的记录：{updMsg.ToJson()}",
                    Description = "CRUD操作"
                });
            }
            return result;
        }

        /// <summary>
        /// 删除 会记录日志
        /// </summary>
        /// <param name="id">主键</param>
        /// <param name="mode">删除模式0假删除 1真删除</param>
        /// <returns>影响行数</returns>
        public int Delete(int id, int mode = 0)
        {
            var count = 0;

            if (mode == 0)
            {
                var entity = DBC.QueryFirst<SysLog>(id);
                if (entity != null)
                {
                    entity.DeleteTime = DateTime.Now;
                    entity.Deleted = true;
                }
                count = DBC.Update(entity);
            }
            else
            {
                count = DBC.Delete<SysLog>(id);
            }

            if (count > 0)
            {
                RecordBehavior(new RLogBehavior
                {
                    Table = nameof(SysLog),
                    Method = mode == 0 ? EntityMethod.M_删除 : EntityMethod.M_销毁,
                    Content = mode == 0 ? $"删除了{count}条记录，ID为：{id}" : $"销毁了{count}条记录，ID为：{id}",
                    Description = "CRUD操作"
                });
            }
            return count;
        }

        /// <summary>
        /// 删除 使用拉姆达 会记录日志
        /// </summary>
        /// <param name="ids">主键</param>
        /// <param name="mode">删除模式0假删除 1真删除</param>
        /// <returns>影响行数</returns>
        public int Delete(List<int> ids, int mode = 0)
        {
            var count = 0;

            if (mode == 0)
            {
                count = DBC.Update<SysLog>()
                  .Set(f => f.Deleted, true)
                  .Set(f => f.DeleteTime, DateTime.Now)
                  .Where(w => ids.Contains(w.Id)).ExecuteAffrows();
            }
            else
            {
                count = DBC.Delete<SysLog>(w => ids.Contains(w.Id));
            }

            if (count > 0)
            {
                RecordBehavior(new RLogBehavior
                {
                    Table = nameof(SysLog),
                    Method = mode == 0 ? EntityMethod.M_删除 : EntityMethod.M_销毁,
                    Content = mode == 0 ? $"删除了{count}条记录，ID为：{ids.ToJson()}" : $"销毁了{count}条记录，ID为：{ids.ToJson()}",
                    Description = "CRUD操作"
                });
            }
            return count;
        }

        /// <summary>
        /// 获取单个  可设置记录日志 详情接口请记录日志 其他业务使用不要记录日志
        /// </summary>
        /// <param name="id">主键</param>
        /// <param name="recordLog">设置记录日志</param>
        /// <returns>数据实体</returns>
        public SysLog Detail(int id, bool recordLog = false)
        {
            var result = DBC.QueryFirst<SysLog>(id);
            if (result != null && recordLog)
            {
                RecordBehavior(new RLogBehavior
                {
                    Table = nameof(SysLog),
                    Method = EntityMethod.M_查看,
                    Content = $"查看了ID为：{id}的记录",
                    Description = "CRUD操作"
                });
            }
            return result;
        }

        #endregion

    }
}


